/* 封装本地存储的操作     */
const TOKEN_NAME = 'hm-news-token'
const CHANNEL_NAME = 'hm-news-channel-list'
const SEARCH_HISTORY_NAME = 'hm-news-search-list'

export const setStorageToken = (payload) => {
  localStorage.setItem(TOKEN_NAME, JSON.stringify(payload))
}

export const getStorageToken = () => {
  return JSON.parse(localStorage.getItem(TOKEN_NAME)) || {}
}

export const removeStorageToken = () => {
  localStorage.removeItem(TOKEN_NAME)
}

// 封装未登录时本地存储我的频道
export const setStorageChannel = (payload) => {
  localStorage.setItem(CHANNEL_NAME, JSON.stringify(payload))
}

// 封装获取本地存储中我的频道数据
export const getStorageChannel = () => {
  // 不需要设置默认值空对象,因为一旦本地中获取不到频道的数据，就会发送请求来获取频道的数据
  return JSON.parse(localStorage.getItem(CHANNEL_NAME))
}

// 存搜索的历史记录
export function setStroageSearchHistory(payload) {
  localStorage.setItem(SEARCH_HISTORY_NAME, JSON.stringify(payload))
}

export function getStroageSearchHistory() {
  return JSON.parse(localStorage.getItem(SEARCH_HISTORY_NAME)) || []
}
